import {
    circumstancePayment,
    getFlowWorkerList,
    stopPay,
    stopAllPay,
    exportWorkerPriceList
} from '../../api/pay'
import user from '../../../utils/user.js'
import {
    dateFormat
} from '../../../utils/util.js'
const app = getApp()
Page({
    data: {
        id: '',
        pageNum: 1,
        notData: false,
        data: {},
        isAdmin: false,
        table: [{
            name: '全部',
            index: 0,
            id: ''
        }, {
            name: '已领薪',
            index: 1,
            id: 1
        }, {
            name: '待领薪',
            index: 2,
            id: 0
        }, {
            name: '停止发薪',
            index: 3,
            id: 2
        }, {
            name: '领薪失败',
            index: 4,
            id: 4
        }, {
            name: '进行中',
            index: 5,
            id: 3
        }],
        title: '筛选',
        cancel: '1',
        screen: false,
        select: 0,
        status: '',
        payMaster: [],
        payMasterTotal:Number,
        personnelNextPage: false,
        projectName: '',
        restatus: [],
        isOperation: false
    },
    onLoad: function (options) {
        this.setData({
            id: options.id,
            isAdmin: user.isAdmin
        })
        user.waitLogin().then(() => {
            this.getFlowWorkerList()
            this.circumstancePayment()
        })
    },
    onUnload(){
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('monitorChange', {data: this.data.isOperation})
    },
    onScroll(event) {
        if (this.data.personnelNextPage) {
            let pageNum = this.data.pageNum
            pageNum += 1
            this.setData({
                // personnelList:[],
                pageNum: pageNum
            }, () => {
                this.circumstancePayment()
            })
        }
    },
    exportWorkerPriceList() {
        exportWorkerPriceList({
            processId: this.data.id
        }).then(res => {
            const url = `${app.globalData.baseUrl}/common/download?fileName=${encodeURIComponent(res)}&delete=false`
            if (wx.canIUse('openDocument')) {
                const file = wx.env.USER_DATA_PATH + '/' + this.data.data.projectName.replace(/\//g, '.') + '.xlsx'
                wx.downloadFile({
                    url,
                    filePath: file,
                    success: function () {
                        const documentPath = file
                        wx.openDocument({
                            showMenu: true,
                            filePath: documentPath,
                            fileType: 'xlsx'
                        })
                    }
                })
            } else {
                this.setData({
                    copyLinkStatus: true,
                    copyLinkContent: url
                })
            }
        })
    },
    onCall(event) {
        console.log(event, '电话电话电话')
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.phone
        })
    },
    onChangeSelect(event) {
        console.log(event, '呵呵呵呵呵')
        this.setData({
            select: event.currentTarget.dataset.index,
            status: event.currentTarget.dataset.id,
            screen: false,
            payMaster: [],
            pageNum: 1,
            payMasterTotal: null * 1
        }, () => {
            this.circumstancePayment()
            this.getFlowWorkerList()
        })
    },
    onClose(event) {
        console.log(event, '子组件回复的内容')
        this.setData({
            screen: false
        })
    },
    onScreen(event) {
        console.log(event, '哈哈哈哈哈')
        this.setData({
            screen: true
        })
    },
    onStop(event) {
        app.confirm('是否确认停止该员工发薪').then(() => {
            stopPay({
                id: event.currentTarget.dataset.id
            }).then(() => {
                app.toast('停止发薪成功', 'success')
                setTimeout(() => {
                    this.setData({
                        payMaster: [],
                        pageNum: 1,
                        data:{},
                        notData: false,
                        payMasterTotal: null * 1,
                        isOperation:true
                    },()=>{
                    this.circumstancePayment()
                    this.getFlowWorkerList()
                    })
                }, 1000)
                
            })
        })
    },
    onStopAll() {
        app.confirm('是否确认停止该订单内所有员工发薪，已提现或进行中的员工不会影响').then(() => {
            console.log('此接口有时候会传递undefined给后端')
            stopAllPay({
                processId: this.data.id ? this.data.id : null
            }).then(res => {
                app.alert(res).then(() =>{
                  setTimeout(() => {
                    this.setData({
                        payMaster: [],
                        pageNum: 1,
                        data:{},
                        notData: false,
                        restatus:[],
                        payMasterTotal: null * 1,
                        isOperation:true
                    },()=>{
                    this.circumstancePayment()
                    this.getFlowWorkerList()
                    })
                }, 1000)
                })
            })
        })

    },
    // onTelephone(event) {
    //     if (event.currentTarget.dataset.phone)
    //         wx.makePhoneCall({
    //             phoneNumber: event.currentTarget.dataset.phone
    //         })
    // },

    onJump(event) {
        console.log(event, 'onJumponJump')
        if (event.currentTarget.dataset.status !== 0) {
            let id = event.currentTarget.dataset.id
            wx.navigateTo({
                url: '/packagePay/pages/individual-detail/individual-detail?id=' + id,
            })
        }
    },
    async getFlowWorkerList() {
        const result = await getFlowWorkerList({id: this.data.id || null,})
        this.setData({
            data: result
        })
    },
    circumstancePayment() {
        circumstancePayment({
            processId: this.data.id,
            status: this.data.status === '' ? '' : this.data.status,
            pageSize: 10,
            pageNum: this.data.pageNum
        }).then((res) => {
            console.log(res, '支付情况')
            if (!res.personnelNextPage) {
                this.data.personnelNextPage = false
            } else {
                this.data.personnelNextPage = true
            }
            res.list.map(item => {
                item.idCardNo = item.idCardNo.slice(14, 18)
            })
            const arr = res.list.filter(item => item.status == 0 | item.status == 4)
            let restatus = []
            // restatus.push(...this.data.restatus)
            restatus.push(...arr)
            console.log(restatus, 'restatusrestatus')
            let list = []
            list.push(...this.data.payMaster)
            list.push(...res.list)
            this.setData({
                payMaster: list,
                payMasterTotal: res.totalCount,
                personnelNextPage: res.hasNextPage,
                projectName: res.list[0].projectName,
                restatus: restatus
            })
        })
    },
})